package com.zk.babala;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.zk.babala.datasource.YingHuaSource;
import com.zk.babala.handler.ExceptionHandler;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.LineUtil;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
 * @Author: zhangke
 * @Date: 2022/3/31 14:33
 */
public class BabalaApp extends Application {
    private static Context context;
    private static ISourceParse sourceParse;


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        ExceptionHandler exceptionHandler = ExceptionHandler.getInstance();
        exceptionHandler.init(context);
        handleSSLHandshake();
        initUrlParse();
    }

    public static Context getContext() {
        return context;
    }

    public static ISourceParse getSourceParse() {
        return sourceParse;
    }

    public static void initUrlParse() {
        try {
            if (LineUtil.getLineUtil().getLineSource().isEmpty()) {
                LineUtil.getLineUtil().setLine(YingHuaSource.class.getSimpleName(), YingHuaSource.defaultUrl);
            }
            sourceParse = (ISourceParse) Class.forName("com.zk.babala.datasource." + LineUtil.getLineUtil().getLineSource()).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }


    private static void handleSSLHandshake() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }

            }};
            SSLContext sc = SSLContext.getInstance("TLS");
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (Exception ignored) {
        }

    }

    public static void restartApp(Context context) {
        initUrlParse();
        Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            // 清除任务栈并启动新的任务
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(intent);
        }
    }

}
